package com.chushouya.manager.service.admin.impl;

import com.chushouya.manager.dao.entity.RoleEntity;
import com.chushouya.manager.dao.entity.UserRoleEntity;
import com.chushouya.manager.dao.repository.RoleRepository;
import com.chushouya.manager.dao.repository.UserRoleRepository;
import com.chushouya.manager.dto.admin.userrole.UserRoleDTO;
import com.chushouya.manager.service.admin.UserRoleService;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleRepository userRoleRepository;

    @Resource
    private RoleRepository roleRepository;

    @Override
    public void insertUserRole(Long adminUserId, List<Long> roleIdList) {
        if (Lists.isNotEmpty(roleIdList)) {
            List<UserRoleEntity> list = new ArrayList<>();
            for (Long roleId : roleIdList) {
                UserRoleEntity userRoleEntity = new UserRoleEntity();
                userRoleEntity.setAdminUserId(adminUserId);
                userRoleEntity.setRoleId(roleId);
                list.add(userRoleEntity);
            }
            userRoleRepository.insertList(list);
        }
    }

    @Override
    public void deleteUserRole(Long adminUserId) {
        Check.notNull(adminUserId, "adminUserId can not be null");
        userRoleRepository.deleteUserRole(adminUserId);
    }

    @Override
    public List<Long> selectUserRoleIdList(Long adminUserId) {
        List<UserRoleEntity> userRoleList = userRoleRepository.selectUserRole(adminUserId);
        if (Lists.isEmpty(userRoleList)) {
            return Lists.emptyList();
        }
        return userRoleList.stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList());
    }

    @Override
    public List<Long> selectUserIdList(Long roleId) {
        List<UserRoleEntity> userRoleList = userRoleRepository.selectUserRoleByRoleId(roleId);
        if (Lists.isEmpty(userRoleList)) {
            return Lists.emptyList();
        }
        return userRoleList.stream().map(UserRoleEntity::getAdminUserId).collect(Collectors.toList());
    }

    @Override
    public List<UserRoleDTO> selectAllUserRoleList() {
        List<UserRoleEntity> userRoleEntityList = userRoleRepository.selectAll();
        List<RoleEntity> roleEntityList = roleRepository.selectAll();
        if (Lists.isEmpty(userRoleEntityList) || Lists.isEmpty(roleEntityList)) {
            return Lists.emptyList();
        }
        List<UserRoleDTO> userRoleList = new ArrayList<>();
        for (UserRoleEntity userRoleEntity : userRoleEntityList) {
            UserRoleDTO userRoleDTO = new UserRoleDTO();
            userRoleDTO.setAdminUserId(userRoleEntity.getAdminUserId());
            userRoleDTO.setRoleId(userRoleEntity.getRoleId());
            RoleEntity roleEntity = roleEntityList.stream()
                    .filter(role -> role.getRoleId().equals(userRoleEntity.getRoleId()))
                    .findFirst()
                    .orElse(null);
            if (Objects.nonNull(roleEntity)) {
                userRoleDTO.setRoleName(roleEntity.getRoleName());
            } else {
                userRoleDTO.setRoleName("Unknown Role");
            }
            userRoleList.add(userRoleDTO);
        }
        return userRoleList;
    }
}
